#!/usr/bin/env python
# -*- coding:utf-8 -*-
from sqlalchemy import and_

from app import db
from utils.response_code import RET, error_map_EN
from utils import commons
from controller.workerInfoController import WorkerInfoController


class WorkerInfoService(WorkerInfoController):
    # 获取空闲维修师傅信息
    @classmethod
    def freeWorkerInfo(cls, **kwargs):
        try:
            # 获取的是空闲且没删除的师傅
            worker_info = db.session.query(cls).filter(and_(cls.status == 0, cls.isDelete == 0)).all()
        except Exception as e:
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': "数据库连接异常,登录失败"}}
        worker_info_data_list = commons.query_to_dict(worker_info)
        result = []
        for i in worker_info_data_list:
            worker_info_item = {
                'workerID': i.get('workerID'),
                'workerName': i.get('workerName'),
                'workerPhone': i.get('workerPhone')
            }
            result.append(worker_info_item)
        return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': result}

    # 添加师傅
    @classmethod
    def add_worker_info(cls, **kwargs):
        # 添加记录
        try:
            # 添加时默认师傅是空闲的，且isDelete=0,repairID=0默认没有维修订单
            worker_info = {
                'workerName': kwargs.get('workerName'),
                'workerPhone': kwargs.get('workerPhone'),
                'status': 0,
                'isDelete': 0,
                'repairID': 0
            }
            WorkerInfoController.add(**worker_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': error_map_EN[RET.OK]}
        except:
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': '数据库添加错误'}}

    # 删除师傅
    @classmethod
    def del_worker_info(cls, **kwargs):
        # 删除师傅只需要修改isDelete字段
        try:
            if not kwargs.get('workerID'):
                return {'code': RET.DATAERR, 'message': error_map_EN[RET.DATAERR], 'data': {'error': '没有workerID'}}
            worker_info = {
                'workerID': kwargs.get('workerID'),
                'isDelete': 1
            }
            WorkerInfoController.update(**worker_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': error_map_EN[RET.OK]}
        except:
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': '数据库删除失败'}}

    # 返回所有师傅的信息
    @classmethod
    def get_all_worker_info(cls, **kwargs):
        try:
            worker_infos = db.session.query(cls).all()
        except Exception as e:
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'data': {'error': "数据库连接异常,获取数据失败"}}
        worker_infos_data_list = commons.query_to_dict(worker_infos)
        result = []
        #获取维修信息
        for i in worker_infos_data_list:
            worker_info_item = {
                'workerID': i.get('workerID'),
                'workerName': i.get('workerName'),
                'workerPhone': i.get('workerPhone'),
                'status': i.get('status'),
                'isDelete': i.get('isDelete'),
                'repairID': i.get('repairID'),
                'createTime': i.get('createTime')
            }
            #数据添加
            result.append(worker_info_item)

        return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'data': result}
